#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define DU double
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define rep(i,x,n) for(int i=(x);i<=(n);i++)
#define nep(i,x,n) for(int i=(x);i>=(n);i--)
using namespace std;
const int N=2e5+10,M=19;
ll n,m,K,a[N],b[N],c[N],d[M][N],X[N],f[M][N],p2[N];
char s[M][N];
inline ll read(){
	ll s=0,f=1;
	char ch=getchar();
	while(!isdigit(ch)){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(isdigit(ch)){
		s=(s<<3)+(s<<1)+(ch^48);
		ch=getchar();
	}
	return s*f;
}
inline void print(ll x){
	if(x<0){putchar('-');x=-x;}
	if(x>9) print(x/10);
	putchar(x%10+'0');
}
inline void Solve(){
	rep(i,0,3) X[i]=read();
	rep(i,1,n) a[i]=(b[i]^X[i%4]);
	bool _=1;
	rep(i,1,m){
		bool tmp=0;
		rep(j,0,20){
			if(c[i]==p2[j]) tmp=1;
		}
		_&=tmp;
	}
	if(_){
		rep(i,1,n) f[0][i]=i;
		rep(i,1,K){
			rep(j,1,p2[K-i]){
				int x=f[i-1][j*2-1],y=f[i-1][j*2];
				if(!d[i][j]){
					if(a[x]>=i) f[i][j]=x;
					else f[i][j]=y;
				}
				else{
					if(a[y]>=i) f[i][j]=y;
					else f[i][j]=x;
				}
			}
		}
		ll res=0;
		rep(i,1,m){
			int y;
			rep(j,0,20) if(p2[j]==c[i]) y=j;
			res=(res^(i*f[y][1]));
		}
		print(res),puts("");
		return ;
	}
}
int main(){
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	n=read(),m=read();rep(i,1,n) b[i]=read();rep(i,1,m) c[i]=read();
	p2[0]=1;rep(i,1,20) p2[i]=p2[i-1]*2;
	rep(i,1,n){
		if(p2[i]>=n){
			K=i;break;
		}
	}
	rep(i,1,K){
		scanf("%s",s[i]+1);
		rep(j,1,p2[K-i]) d[i][j]=s[i][j]-'0';
	}
	int Tests=read();
	while(Tests--) Solve();
	return 0;
}